<?php
class DealOrderCodesController extends DealsAppController {

	var $name = 'DealOrderCodes';
	var $helpers = array('Html', 'Form', 'Cms.Index');
	var $components = array('Auth','Session','RequestHandler','Email');
	
	function beforeFilter()
	{
		$this->Auth->allow('index','view');
		parent::beforeFilter();
	}

	function admin_index($dealOrderId = null)
	{
		$this->DealOrderCode->DealOrder->recursive = 0;
		$order = $this->DealOrderCode->DealOrder->findById($dealOrderId);
		if(!$dealOrderId || empty($order))
		{
			$this->errorMsg(__("Invalid URL"));
			$this->redirect("/");
		}
		$conditions = array('DealOrderCode.deal_order_id'=>$dealOrderId);
		$this->pageTitle = sprintf(__("Details of Order No. %s"),$order['DealOrder']['id']);
		if(!isset($this->paginate['conditions']))
		{
			$this->paginate['conditions'] = array();
		}
		$this->paginate['conditions'] = am($this->paginate['conditions'],$conditions);
		$this->paginate['recursive'] = -1;
		$codes = $this->paginate();
		$this->set('codes', $codes);
		$this->set('dealOrder', $order);
	}
	
	
	function admin_changeUsedStatus($id)
	{
		$dealOrder = $this->DealOrderCode->find('first',array('conditions'=>array('id'=>$id,'is_used'=>0),'recursive'=>-1));
		if(empty($dealOrder))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect($this->referer());
			return;
		}
		if($this->DealOrderCode->markAsUsed($id))
		{
			$this->successMsg(__('Deal has been marked as used.'));
		} else {
			$this->errorMsg(__('Deal could not be marked as used.'));
		}
		$this->redirect($this->referer());
	}
	
	
	
}
?>